package org.yxing.palpalgps.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.yxing.palpalgps.AbstractActivity;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.adapter.PhotoGalleryAdapter;
import org.yxing.palpalgps.http.AsyncRequest;
import org.yxing.palpalgps.oauth.OAuth;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;

public class PhotoGalleryActivity extends AbstractActivity implements OnClickListener, OnItemSelectedListener {
	
	private static final String URL_DELETE = "photo/delete";
	private static final String URL_AVATAR = "photo/avatar";
	
	private Gallery mGallery;
	private int mPosition;
	private PhotoGalleryAdapter mAdapter;
	private boolean isReloadPhotoList = false;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photo_gallery);
		setTitle(getString(R.string.photo_view));
		
		mGallery = (Gallery) findViewById(R.id.photoGallery);
		
		ArrayList<String> data = getIntent().getStringArrayListExtra("data");
		mPosition = getIntent().getIntExtra("id", 0);
		mAdapter = new PhotoGalleryAdapter(this, data);
		mGallery.setAdapter(mAdapter);
		mGallery.setSelection(mPosition);
		
		mGallery.setOnItemSelectedListener(this);
		
		findViewById(R.id.buttonDelete).setOnClickListener(this);
		findViewById(R.id.buttonAvatar).setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.buttonDelete:
			delete();
			break;
		case R.id.buttonAvatar:
			setAvater();
			break;
		}
	}
	
    private void delete() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(getString(R.string.are_you_sure_delete));
        builder.setPositiveButton(getString(R.string.ok), new Dialog.OnClickListener(){
            public void onClick(DialogInterface dialog, int which) {
                
                AsyncRequest resp = new AsyncRequest(PhotoGalleryActivity.this);
                List<BasicNameValuePair> params = OAuth.createAuthParams();
                params.add(new BasicNameValuePair("pid", mAdapter.getItem(mPosition)));
                //delete from server
                resp.fetch(URL_DELETE, params, new AsyncRequest.FetchListener() {
                    
                    public void onComplete(JSONObject response) {
                        isReloadPhotoList = true;
                        mAdapter.delete(mPosition);
                        displayToast(getString(R.string.toast_delete_success));
                    }
                    
                    public void onError(int code, String msg) {
                       displayToast(msg);
                    }
                });
            }
        });
        builder.setNegativeButton(getString(R.string.cancel), null);
        builder.show();
    }
    
    private void setAvater() {
        AsyncRequest resp = new AsyncRequest(PhotoGalleryActivity.this);
        List<BasicNameValuePair> params = OAuth.createAuthParams();
        params.add(new BasicNameValuePair("pid", mAdapter.getItem(mPosition)));
        //delete from server
        resp.fetch(URL_AVATAR, params, new AsyncRequest.FetchListener() {
            
            public void onComplete(JSONObject response) {
                displayToast(getString(R.string.set_as_avatar_success));
            }
            
            public void onError(int code, String msg) {
               displayToast(msg);
            }
        });
    }

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		mPosition = position;
	}

	@Override
	public void onNothingSelected(AdapterView<?> paramAdapterView) {
		// TODO Auto-generated method stub
		
	}
	
	
}
